package com.want.evenchat.controller.activity;

import android.app.Activity;
import android.os.Bundle;
import android.renderscript.RSRuntimeException;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.want.evenchat.R;
import com.want.evenchat.model.Model;
import com.want.evenchat.model.bean.UserInfo;

/**
 * Created by Want on 2018/5/4.
 */

public class AddContactActivity extends Activity{

    private TextView textview_add_find;
    private EditText edittext_add_name;
    private LinearLayout lin_add;
    private TextView textview_add_name;
    private Button button_add_add;
    private UserInfo userInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_addcontact);

        //初始化View
        initView();

        initListener();

    }

    private void initListener() {
        //查找按钮点击监听事件处理
        textview_add_find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                find();
            }
        });

        //添加按钮点击监听事件处理
        button_add_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                add();
            }
        });
    }

    //查找按钮处理
    private void find() {
        //获取输入的用户名称
        final String name = edittext_add_name.getText().toString();

        //检验输入的名称
        if(TextUtils.isEmpty(name)){
            Toast.makeText(AddContactActivity.this, "查找的号码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        //去服务器判断当前用户是否存在
        Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                //去服务器判断当前查找的用户是否存在
                userInfo = new UserInfo(name);//默认服务器有该用户

                //更新UI显示
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        lin_add.setVisibility(View.VISIBLE);

                        textview_add_name.setText(userInfo.getName());

                    }
                });

            }
        });
    }

    //添加按钮处理
    private void add() {
        Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    //去环信服务器添加好友
                    EMClient.getInstance().contactManager().addContact(userInfo.getName() , "添加好友");
                    
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(AddContactActivity.this, "申请成功", Toast.LENGTH_SHORT).show();
                        }
                    });
                } catch (final HyphenateException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(AddContactActivity.this, "添加失败" + e.toString(), Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
    }

    private void initView() {
        textview_add_find = findViewById(R.id.textview_add_find);
        edittext_add_name = findViewById(R.id.edittext_add_name);
        lin_add = findViewById(R.id.lin_add);
        textview_add_name = findViewById(R.id.textview_add_name);
        button_add_add = findViewById(R.id.button_add_add);
    }
}
